package Dao;

import Dao.Impl.BaseDao;
import entity.FitnessEquipment;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class FitnessEquipmentDao extends BaseDao {
    private SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 检索 fitnessEquipment 表中按时间排序的所有器材列表。
     *
     * @return 代表数据库中器材的 FitnessEquipmentManage 对象列表
     */
    public List<FitnessEquipment> queryAllFitnessEquipments() {
        List<FitnessEquipment> FitnessEquipmentList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            String sql = "SELECT * FROM fitness_equipment";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                FitnessEquipment fitnessEquipment = new FitnessEquipment();
                fitnessEquipment.setId(rs.getInt("id"));
                fitnessEquipment.setName(rs.getString("name"));
                fitnessEquipment.setCount(rs.getInt("count"));
                fitnessEquipment.setStatus(rs.getInt("status"));
                fitnessEquipment.setBuy_time(outputFormat.format(rs.getDate("buy_time")));
                fitnessEquipment.setPrice(rs.getDouble(3));
                FitnessEquipmentList.add(fitnessEquipment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseResources(conn, stmt, rs);
        }
        return FitnessEquipmentList;
    }
    /**
     * 将器材添加到 fitnessequipment 表。
     *
     * @param fitness_equipment 代表要添加的器材的 Fitnessequipment 对象
     * @return 如果器材添加成功，则返回 true，否则返回 false
     */
    public boolean addFitnessEquipment(FitnessEquipment fitnessEquipment) {
        boolean flag = false;
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getConnection();
            String sql = "INSERT INTO fitness_equipment (name, buy_time, count, status,price) VALUES (?, ?, ?, ?,?)";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, (fitnessEquipment.getName()));
            stmt.setDate(2, Date.valueOf(fitnessEquipment.getBuy_time()));
            stmt.setInt(3, fitnessEquipment.getCount());
            stmt.setInt(4, fitnessEquipment.getStatus());
            stmt.setDouble(5, fitnessEquipment.getPrice());

            int rows = stmt.executeUpdate();
            if (rows > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseResources(conn, stmt, null);
        }
        return flag;
    }
    /**
     * 从 FitnessEquipmentmanage 表中删除器材。
     *
     * @param id 要删除的器材的 ID
     * @return 如果器材删除成功，则返回 true，否则返回 false
     */
    public boolean deleteFitnessEquipment(int id) {
        boolean flag = false;
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = getConnection();
            String sql = "DELETE FROM fitness_equipment WHERE id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            int rows = stmt.executeUpdate();
            if (rows > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            releaseResources(conn, stmt, null);
        }
        return flag;
    }
}